package pl.edu.pk.mech.kkbus.repository;

import static pl.edu.pk.mech.kkbus.staticstrings.DefectProperty.*;
import static pl.edu.pk.mech.kkbus.staticstrings.NameEntity.NAME_ENTITY_DEFECTS;

import java.util.ArrayList;
import java.util.List;

import pl.edu.pk.mech.kkbus.datastore.common.SynchronizeCommon;
import pl.edu.pk.mech.kkbus.model.Defect;
import pl.edu.pk.mech.kkbus.model.Pojo;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class DefectDAO extends BaseDAO{

	@Override
	public void fillProperty(Pojo pojo, Entity entity) {
		Defect defect = (Defect)pojo;
		  entity.setProperty(DEFECT_DESC, defect.getDescription());
		  entity.setProperty(DEFECT_COSTS, defect.getCosts());
		  entity.setProperty(DEFECT_DATE, defect.getDate());
		  entity.setProperty(DEFECT_ID_BUS, defect.getIdBus());
	}
	
	public List<Defect> getAllDefects() {
		  List<Defect> temp = new ArrayList<Defect>();
		  Iterable<Entity> entityList = SynchronizeCommon.listEntities(NAME_ENTITY_DEFECTS, null, null);
		  for (Entity entity : entityList) {
			  temp.add(new Defect(entity));
		  }
		  return temp;
	}
	
	public List<Defect> getDefectsByIdBus(String idBus) {
		  List<Defect> temp = new ArrayList<Defect>();
		  Iterable<Entity> entityList = SynchronizeCommon.listEntities(NAME_ENTITY_DEFECTS, DEFECT_ID_BUS, idBus);
		  for (Entity entity : entityList) {
			  temp.add(new Defect(entity));
		  }
		  return temp;
	}
	
	public Defect getDefect(long id) {
		  Key key = KeyFactory.createKey(NAME_ENTITY_DEFECTS, id);
		  return new Defect(SynchronizeCommon.findEntity(key));
	}
}
